//
//  WeChatMoudle.m
//  eCourseApp
//
//  Created by 朱献国 on 2018/8/7.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "WeChatMoudle.h"
#import <UIKit/UIKit.h>
#import "GDataXMLNode.h"
#import "JZWeChatPayModel.h"

@implementation WeChatMoudle

RCTPromiseResolveBlock resolveBlock = nil;

- (instancetype)init {
  self = [super init];
  if (self) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWeChatPay:) name:@"WeChatPay" object:nil];
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleWeChatPay:(NSNotification *)aNotification {
  NSString *errCode = [aNotification userInfo][@"errCode"];
  resolveBlock(@{@"errCode": errCode});
}

// 向微信注册
RCT_EXPORT_METHOD(registerApp:(NSString *)APP_ID){
  [WXApi registerApp:APP_ID];
}

// 判断是否支持调用微信SDK
RCT_REMAP_METHOD(isSupported,
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject){
  if (![WXApi isWXAppInstalled]) resolve(@NO);
  else resolve(@YES);
}

// 微信支付 必须使用客户端支付
RCT_EXPORT_METHOD(pay:(NSString *)order
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolveBlock  = resolve;
  
  NSData *xmlData = [order dataUsingEncoding:NSUTF8StringEncoding];
//  NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:xmlData options:NSJSONReadingMutableLeaves error:nil];
  
  //使用NSData对象初始化
  GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];

  //获取根节点
  GDataXMLElement *rootElement = [doc rootElement];
  
//  JZWeChatPayModel *weChatPayModel = [[JZWeChatPayModel alloc] init];
  //调起微信支付
  PayReq *req   = [[PayReq alloc] init];
  
  for (int i = 0; i < [rootElement childCount]; i++) {
    
    GDataXMLElement *element = (GDataXMLElement *)[rootElement childAtIndex:i];
    
    NSLog(@"*****Element Name = %@", element.name);
    
    NSLog(@"*****value = %@", element.stringValue);
    
    if ([element.name isEqualToString:@"prepay_id"]) {
      req.prepayId = element.stringValue;
    }
    else if ([element.name isEqualToString:@"nonce_str"]) {
      req.nonceStr = element.stringValue;
    }
    else if ([element.name isEqualToString:@"sign"]) {
      req.sign      = element.stringValue;
    }
    
  }
  
  req.partnerId = @"1493917502"; //[order objectForKey:@"partnerid"];
  req.timeStamp = [[self getNowTimeTimestamp] intValue] / 1000;
  req.package   =  @"Sign=WXPay";//[order objectForKey:@"package"];
  [WXApi sendReq:req];
}

- (NSString *)getNowTimeTimestamp {
  
  NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  
  [formatter setDateStyle:NSDateFormatterMediumStyle];
  
  [formatter setTimeStyle:NSDateFormatterShortStyle];
  
  [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  
  //设置时区,这个对于时间的处理有时很重要
  
  NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  
  [formatter setTimeZone:timeZone];
  
  NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
  
  NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
  
  return timeSp;
}

RCT_EXPORT_MODULE(Wxpay);

@end
